package org.jeecg.modules.workflow.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.mybatis.query.QueryWrapperX;
import org.jeecg.common.workflow.vo.task.instance.BpmProcessInstanceMyPageReqVO;
import org.jeecg.modules.workflow.entity.BpmProcessInstanceExt;

/**
 * 工作流的流程实例的拓展Mapper接口
 *
 * @author whnk
 * @date 2022-04-20
 */
public interface BpmProcessInstanceExtMapper extends BaseMapper<BpmProcessInstanceExt>
{

    default IPage<BpmProcessInstanceExt> selectPage(String userId, BpmProcessInstanceMyPageReqVO reqVO) {
        Page<BpmProcessInstanceExt> page = new Page<>(reqVO.getPageNum(), reqVO.getPageSize());
        return selectPage(page, new QueryWrapperX<BpmProcessInstanceExt>()
                .eqIfPresent("start_user_id", userId)
                .likeIfPresent("name", reqVO.getName())
                .eqIfPresent("process_definition_id", reqVO.getProcessDefinitionId())
                .eqIfPresent("category", reqVO.getCategory())
                .eqIfPresent("status", reqVO.getStatus())
                .eqIfPresent("result", reqVO.getResult())
                .betweenIfPresent("create_time", reqVO.getBeginCreateTime(), reqVO.getEndCreateTime())
                .orderByDesc("id"));
    }

    default BpmProcessInstanceExt selectByProcessInstanceId(String processDefinitionId) {
        return selectOne(new QueryWrapper<BpmProcessInstanceExt>().eq("process_instance_id", processDefinitionId));
    }

    default void updateByProcessInstanceId(BpmProcessInstanceExt updateObj) {
        update(updateObj, new QueryWrapper<BpmProcessInstanceExt>()
                .eq("process_instance_id", updateObj.getProcessInstanceId()));
    }
}
